﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsProjectStandardRepository : BaseRepository<MsProjectStandard>, IMsProjectStandardRepository
    {
        public async Task<MsProjectStandard> GetProjectStandard(Guid projectStandardId, Guid projectGuid)
        {
            return await (from mpsm in Db.MsProjectStandards
                          where mpsm.ProjectStandardId == projectStandardId && mpsm.ProjectGuid == projectGuid
                          select mpsm)
                .AsNoTracking().FirstOrDefaultAsync().ConfigureAwait(false);
        }

        public async Task<List<MsProjectStandardMeasureItemDto>> GetListDetail(Guid projectStandardId)
        {
            return await (
                from h in Db.MsProjectStandardMeasureItems
                join j in Db.MsPointTypeMeasureItems on h.Id equals j.Id
                where h.ProjectStandardId == projectStandardId
                select new MsProjectStandardMeasureItemDto
                {
                    Id = h.Id,
                    MsPointTypeId = j.MsPointTypeId,
                    Code = h.Code,
                    Title = h.Title,
                    SupportCraft = h.SupportCraft,
                    SupportStandard = h.SupportStandard,
                    Type = h.Type,
                    HasDesignValue = h.HasDesignValue,
                    IsChecked = j.IsChecked,
                }).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsProjectStandard>> GetDataByProjects(Guid[] projectIds)
        {
            return await (from mpsm in Db.MsProjectStandards
                          where projectIds.Contains(mpsm.ProjectGuid)
                          select mpsm)
               .AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// 作者：黄钧航
        /// 根据项目Id取对应标准库信息
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <returns></returns>
        public async Task<List<StandardListModel>> GetProjectStandards(Guid projectId)
        {
            return await (from h in Db.MsProjectStandards
                          where h.ProjectGuid == projectId
                          select new StandardListModel
                          {
                              ProjectStandardId = h.ProjectStandardId,
                              Code = h.Code,
                              Title = h.Title,
                              Classes = h.Content.ToObject<List<StandardClass>>(),
                              Templates = h.Content.ToObject<List<StandardClass>>(),
                              ProjectGuid = h.ProjectGuid,
                              CompanyGuid = h.CompanyGuid,
                          }).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsProjectStandard>> GetDataByProjectsWithStandardId(Guid[] projectIds, Guid excludeStandard)
        {
            return await (from mpsm in Db.MsProjectStandards
                          where projectIds.Contains(mpsm.ProjectGuid) && (mpsm.ProjectStandardId != excludeStandard || excludeStandard == Guid.Empty)
                          select mpsm)
               .AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// 根据工程节点名称
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <param name="projectId">项目Id</param>
        /// <param name="stageName">工程阶段名</param>
        /// <returns></returns>
        public async Task<MsProjectStandard> GetProjectStandardByName(Guid companyId, Guid projectId, string stageName)
        {
            return await (from mpsm in Db.MsProjectStandards
                          where mpsm.CompanyGuid == companyId
                          && mpsm.ProjectGuid == projectId
                          && mpsm.Stage == stageName
                          select mpsm)
                .AsNoTracking().FirstOrDefaultAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// 同步项目标准库
        /// </summary>
        public async Task SyncProjectStandard(Guid projectId, Guid userId)
        {
            await Db.Database.ExecuteSqlRawAsync($"SELECT sync_project_standard_from_base('{projectId}'::uuid, '{userId}'::uuid);");
        }
    }
}